//
//  AY_MainTabViewController.m
//  AiYue
//
//  Created by mac on 2023/10/9.
//

#import "AY_MainTabViewController.h"
#import "AY_BookShelfViewController.h"
#import "AY_BookCityPage.h"
#import "AY_RankListPage.h"
#import "AY_RankHomeViewController.h"
#import "AY_BookMyPage.h"
#import "AY_BookSortPage.h"
#import "AY_QiuShuView.h"

@interface AY_MainTabViewController ()

@property (nonatomic, strong) AY_QiuShuView *qiuShuView;
@end

@implementation AY_MainTabViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getQiuShuInfo) name:REFRECommend object:nil];
    /* 设置背景色 */
    [self.tabBar setBarTintColor:[UIColor whiteColor]];
    self.tabBar.translucent = NO;
    self.tabBar.backgroundImage = [[UIImage alloc]init];
    self.tabBar.shadowImage = [[UIImage alloc]init];
    // 添加子控制器
    [self addChildVCs];
    [self config];
}

- (void)getQiuShuInfo {
    [AYApiRequest getQiuShuInfoSuccess:^(AY_QiuShuModel * _Nonnull pkg) {
        if (!kStringIsEmpty(pkg.data.bookId)) {
            self.qiuShuView.model = pkg.data;
            [[AYTool lastWindow] addSubview:self.qiuShuView];
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (void)config {
    self.tabBar.backgroundColor = [UIColor whiteColor];

    self.tabBar.barStyle = UIBarStyleBlack;
    [UITabBar appearance].translucent = NO;
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    
    self.tabBar.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, -1);
    self.tabBar.layer.shadowOpacity =0.1;
}

- (void)addChildVCs {
    [self addChildVC:[AY_BookShelfViewController new] title:@"书架" imageName:@"tabbar_sj" tag:1];
    [self addChildVC:[AY_BookCityPage new] title:@"书城" imageName:@"tabbar_sc" tag:2];
    [self addChildVC:[AY_BookSortPage new] title:@"分类" imageName:@"tabbar_fl" tag:3];
    [self addChildVC:[AY_RankHomeViewController new] title:@"排行榜" imageName:@"tabbar_ph" tag:4];
    [self addChildVC:[AY_BookMyPage new] title:@"我的" imageName:@"tabbar_wd" tag:5];
}

- (void)addChildVC:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName tag:(NSInteger)tag{
    vc.tabBarItem.title = title;
    vc.tabBarItem.tag = tag;
    vc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_d", imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIFont *itemFont = YFont(10);
    //标题在不同状态下的颜色
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:COLOR(@"000002"),NSFontAttributeName:itemFont} forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:THEME_COLOR,NSFontAttributeName:itemFont} forState:UIControlStateSelected];
    [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    
    [self addChildViewController:vc];
}

- (AY_QiuShuView *)qiuShuView {
    if (!_qiuShuView) {
        _qiuShuView = [[AY_QiuShuView alloc] initWithFrame:CGRectMake(0, SCREEN_H - GK_TABBAR_HEIGHT - YNUM(16) - YNUM(140), SCREEN_W, YNUM(140))];
    }
    return _qiuShuView;
}
@end
